'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 离线奖励
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var offlineConf = require('../../../config/offlineConf.js');
var rewardService = require('../../../services/rewardService');
var constantUtil = require('../../../util/constantUtil.js');
var buildType = require('../../../consts/buildType.js');
var userModel = require('../../../models/userModel.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 领取离线奖励
 */
Handler.prototype.receive = function (msg, session, next) {
    var role_id = session.uid;

    var player_manager = new PlayerManager(role_id);
    var reward_list = [];
    var user_model;
    var last_time = 0;

    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'arena', 'expedition', 'online', 'activity', 'bag', 'build', 'soldier', 'task', 'achieve', 'daily_task', 'tech', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var uid = player_manager.role.uid;
                userModel.getByUid(uid, cb);
            },
            function (model, cb) {
                var role_model = player_manager.role;

                user_model = model;
                var login_time = moment(user_model.last_login_time).unix();
                var leave_time = role_model.last_logout_time;
                if (login_time > leave_time) {
                    last_time = login_time;
                } else {
                    last_time = leave_time;
                }
                var diff_day = parseInt((moment().unix() - last_time) / (24 * 3600));
                var build_model = player_manager.build;
                var build = build_model.getBuildByType(buildType.NEXUS);
                if (!build) {
                    return next(null, {
                        code: Code.BUILD_NOT_EXIST
                    });
                }
                var nexus_lv = build.lv;
                var reward_all = offlineConf.getConf(nexus_lv, diff_day);

                // 发放奖励
                if (diff_day >= 3) {
                    for (var i = 0; i < reward_all.day_ratio; i++) {
                        reward_list = reward_all.reward_list;
                        rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ENTRYROLE, operationType.OPR_LEAVE_REWARD);
                    }
                }
                player_manager.save(cb);
            },
            function (cb) {
                user_model.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
